package com.cls.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.enums.DictEnum;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import com.cls.system.entity.Configure;
import com.cls.system.service.IConfigureService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置表 Controller
 *
 * @author slx
 * @date 2020-11-17 11:51:11
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ConfigureController extends BaseController {

    private final IConfigureService configureService;
    private final ISysDictService sysDictService;

    /**
    * 配置表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure")
    public String configureIndex(Model model){
        Configure configure =new Configure();
        List<Configure> configures = configureService.findConfigures(configure);
        if(configures.size()>0){
            model.addAttribute("configure",configures.get(0));
        }else {
            configure.setProportion(0.0);
            model.addAttribute("configure",configure);
        }
        //字典表
    /*    List<SysDict> topicTypeLis=new ArrayList<>();
        List<SysDict> parentTypeList = sysDictService.findByParentCode(DictEnum.TOPIC.getCode());
        for (SysDict dic:parentTypeList) {
            List<SysDict> childrenTypeList = sysDictService.findByParentCode(dic.getCode());
            topicTypeLis.addAll(childrenTypeList);
        }
        System.out.println(topicTypeLis);
        model.addAttribute("topicTypeLis",topicTypeLis);*/

        return MyUtil.view("business/configure/configure");
    }

    /**
    * 配置表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure/configureAdd")
    public String configureAdd(Model model){
        return MyUtil.view("business/configure/configureAdd");
    }

    /**
    * 配置表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure/configureUpdate/{configureId}")
    public String configureUpdate(@PathVariable Long configureId, Model model){
        Configure configure = configureService.getById(configureId);
        model.addAttribute("configure",configure);
        return MyUtil.view("business/configure/configureUpdate");
    }



    /**
    * 配置表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/configure/configureDetail/{configureId}")
    public String configureDetail(@PathVariable Long configureId, Model model){
        Configure configure = configureService.getById(configureId);
        model.addAttribute("configure",configure);
        return MyUtil.view("business/configure/configureDetail");
    }

    @GetMapping("configure")
    @ResponseBody
    @RequiresPermissions("configure:list")
    public ResponseVO getAllConfigures(Configure configure) {
        return ResponseVO.success(configureService.findConfigures(configure));
    }

    @GetMapping("configure/list")
    @ResponseBody
    @RequiresPermissions("configure:list")
    public ResponseVO configureList(QueryRequest request, Configure configure) {
        PageInfo<Configure> pageInfo =  this.configureService.findConfigures(request, configure);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增Configure", exceptionMessage = "新增Configure失败")
    @PostMapping("configure")
    @ResponseBody
    @RequiresPermissions("configure:add")
    public ResponseVO addConfigure(@Valid Configure configure) {
        this.configureService.createConfigure(configure);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除Configure", exceptionMessage = "删除Configure失败")
    @PostMapping("configure/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("configure:delete")
    public ResponseVO deleteConfigures(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.configureService.deleteConfigures(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个Configure", exceptionMessage = "删除Configure失败")
    @PostMapping("configure/delete/{configureId}")
    @ResponseBody
    @RequiresPermissions("configure:delete")
    public ResponseVO deleteConfigure(@PathVariable Long configureId) {
        this.configureService.removeById(configureId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Configure", exceptionMessage = "修改Configure失败")
    @PostMapping("configure/update")
    @ResponseBody
    public ResponseVO updateConfigure(@Valid Configure configure) {
        configure.setSnapshot(false);
        this.configureService.updateConfigure(configure);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Configure", exceptionMessage = "导出Excel失败")
    @PostMapping("configure/excel")
    @ResponseBody
    @RequiresPermissions("configure:export")
    public void export(QueryRequest queryRequest, Configure configure, HttpServletResponse response) {
        List<Configure> configures = this.configureService.findConfigures(queryRequest, configure).getList();
        ExcelKit.$Export(Configure.class, response).downXlsx(configures, false);
    }
}
